<?php

/**
  Permission class.
  Author: Hiếu Phạm Trung
  Date Edit: 1/2/2014
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class permission extends CI_Model {

    var $group_pri = array();
    var $res_data = array();
    var $data = array();
    var $user_login = '';
    var $user_id = 0;
    var $isLogin = 0;
    var $user_group = 0;
    public $mytable = "privileges";
    public $primaryKey = "PRI_ID";
    public $resCode = "PRIVILEGE";

    /**
      Permission level:
      Permission one level
      @Action,	@Level
      View:		1
      Insert:		3
      Update:		5
      Delete:		10
      Full:		19

      if @Action > 2 then
      @Level = (@Level of @Action1) + (@Level of @Action2) + .. +(@Level of @Actionn)
      endif

      Exp:
      @Action,			@Level
      View + Insert		4
      View + Update			6
      View + Delete		11


      @Action,			@Level
      Insert + Delete		13
      ...............		..

     */
    function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model("muser");
        $this->load->database();
    }

    /**
      @param:
      $action: select, insert, delete, update
      $resCode: define in resources table
      $userID: if $userID == 0 then $userID = current user id
      @return: 1 if true
     */
    function check($action = "select", $resCode = "", $userID = 0) {
		if ($userID <= 0)
           $userID = $this->madmin->adm_id;
        if (!in_array($userID,$this->group_pri)) {
			$this->get_permission_array($userID);
        }
		
        if (!empty($this->group_pri[$userID][$resCode]))
            $level = $this->group_pri[$userID][$resCode];
        else
            return 0;

        if ($level == 0)
            return 0;
		if ($action=="full" &&$level < 19 )
            return 0;
        if ($level == 19 )
            return 1;


        $arr_select = array('1', '4', '6', '9', '11', '14', '16');
        $arr_insert = array('3', '4', '8', '9', '13', '14', '18');
        $arr_delete = array('10', '11', '13', '14', '15', '16', '18');
        $arr_update = array('5', '6', '8', '9', '15', '16', '18');

        if (($action == "select" && in_array($level, $arr_select)) || ($action == "insert" && in_array($level, $arr_insert)) ||
                ($action == "delete" && in_array($level, $arr_delete)) || ($action == "update" && in_array($level, $arr_update)))
            return 1;
        return 0;
    }

    public function checkLogin() {
        return $this->session->userdata('isLogin');
    }

    function setEmpty() {
        $this->group_pri = array();
    }

    function get_permission_array($userID) {
	
		if (in_array($userID,$this->group_pri)) {
			return $this->group_pri[$userID];
        }
		
        $this->db->from("privileges as p");
        $this->db->where("ADM_ID", $userID);
        $this->db->join("resource as r", "p.RES_ID = r.RES_ID");
        $this->db->order_by("RES_ORDER asc");
        $privilegesRow = $this->db->get()->result();
		$this->group_pri[$userID] = array();
		$this->res_data[$userID] = array();
        if ($privilegesRow != NULL) {
            foreach ($privilegesRow as $r) {
				if($r->PRI_LEVEL >0){
					$this->group_pri[$userID][$r->RES_CODE] = $r->PRI_LEVEL;
					$this->res_data[$userID][$r->RES_CODE] = $r;
				}
            }
            return $this->group_pri[$userID];
        } else {
            return false;
        }
    }
	
	function get_resData($userID){
		if (in_array($userID,$this->res_data)) {
			return $this->res_data[$userID];
        }
		$this->get_permission_array($userID);
		return $this->res_data[$userID];
	}
    function get($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->first_row();
    }

    function field() {
        $fieldData = array();
        return $fieldData;
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = $this->primaryKey;

        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);

        $this->db->from($this->mytable);
        return $this->db->get()->result();
    }

}

?>